import pendulum


def get_message_data(page):
    all_message = []
    all_img = []
    while True:
        if page.s_ele('已经到顶啦'):
            break
        page('.messageList').scroll.to_top()
        page.wait(1)
    kehu_name = page.s_ele('@data-qa-id=qa-user-portrait-username').text
    message_list = page.s_eles('.msgItemWrap')
    for message in message_list:
        message_row = {
            'senderPhotoUrl': '',
            'sendName': '',
            'sendTime': '',
            'sendTextContent': '',
            'sendObjectUrl': '',
            'sendMsgType': 0
        }
        kehu_message = message.s_ele('.iD7SHBvMhm4OhfCsBGr1 leaveMessage messageNotMe')
        kefu_message = message.s_ele('.iD7SHBvMhm4OhfCsBGr1 leaveMessage messageIsMe')
        kehu_img = message.s_ele(r'@style=position: relative; display: flex; flex-direction: row;')
        kefu_img = message.s_ele(r'@style=position: relative; display: flex; flex-direction: row-reverse;')
        system_message = message.s_ele(
            '.chatd-text chatd-text--breakWord chatd-text--truncate chatd-card-header-content')
        time_message = message.s_ele('.O4UWWFoQxgMq4AWHMq25')
        if kehu_message and kehu_message.text != '':
            message_row['sendName'] = kehu_name
            message_row[
                'sendTime'] = time_message.text if '昨天' not in time_message.text else pendulum.now().subtract(
                days=1).strftime('%m月%d日 ') + time_message.text.replace('昨天 ', '')
            message_row['sendTextContent'] = kehu_message.text
            all_message.append(message_row)
            # all_message +=f'{kehu_name}:' + kehu_message.text +'' + time_message.text + '\n'
        if kehu_img and not kehu_message:
            img = kehu_img.child(1)
            if img and img.attr("src"):
                src = img.attr("src")
                # all_message += f'{kehu_name}:' + src +'' + time_message.text + '\n'
                all_img.append(src)
        if kefu_message:
            kefu_name_ele = message.s_ele('.XKc_5xgSwz4XGLjzWOGa')
            if kefu_name_ele:
                kefu_name = kefu_name_ele.text
                message_row['sendName'] = kefu_name
                message_row[
                    'sendTime'] = time_message.text if '昨天' not in time_message.text else pendulum.now().subtract(
                    days=1).strftime('%m月%d日 ') + time_message.text.replace('昨天 ', '')
                message_row['sendTextContent'] = kefu_message.text
                all_message.append(message_row)
                # all_message += f'{kefu_name}:' + kefu_message.text +'' + time_message.text + '\n'
            else:
                message_row['sendName'] = '客服(连续消息)'
                message_row[
                    'sendTime'] = time_message.text if '昨天' not in time_message.text else pendulum.now().subtract(
                    days=1).strftime('%m月%d日 ') + time_message.text.replace('昨天 ', '')
                message_row['sendTextContent'] = kefu_message.text
                all_message.append(message_row)
                # all_message += kefu_message.text +'' + time_message.text + '\n'
        if kefu_img and not kefu_message:
            img = kefu_img.child(1)
            kefu_name_ele = message.s_ele('.XKc_5xgSwz4XGLjzWOGa')
            if img and img.attr("src"):
                src = img.attr("src")
                all_img.append(src)
                # if kefu_name_ele:
                #     kefu_name = kefu_name_ele.text
                #     # all_message += f'{kefu_name}:' + src +'' + time_message.text + '\n'
                #     all_img.append(src)
                # else:
                #     # all_message += src +'' + time_message.text + '\n'
                #     all_img.append(src)
        if system_message:
            message_row['sendName'] = '系统消息'
            message_row[
                'sendTime'] = time_message.text if '昨天' not in time_message.text else pendulum.now().subtract(
                days=1).strftime('%m月%d日 ') + time_message.text.replace('昨天 ', '')
            message_row['sendTextContent'] = system_message.text
            all_message.append(message_row)
    return all_message, all_img
